package com.panshi.controller;

import com.panshi.pojo.Departement;
import com.panshi.pojo.Employee;
import com.panshi.service.impl.DepartementServiceImpl;
import com.panshi.service.impl.EmployeeServiceImpl;
import com.panshi.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 员工管理
 */
@Controller
public class EmployeeController {

    @Autowired
    EmployeeServiceImpl employeeService;
    //
    @Autowired
    DepartementServiceImpl departementService;

    @Autowired
    RedisUtil redisUtil;


    @RequestMapping("emps")
    public String list(Model model) {
        Collection<Employee> employees = employeeService.getAll();
        System.out.println(employees);
        model.addAttribute("emps", employees);
        return "/emp/list";
    }

    @GetMapping("/emp")
    public String toAdd(Model model) {
        Collection<Departement> departments = departementService.getDepartments();
        model.addAttribute("departments", departments);
        return "/emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee) {
        System.out.println(employee.toString() + " ============");
        employeeService.save(employee);
        return "redirect:/emps";
    }


    @RequestMapping("/update")
    public String toUpdateEmp(@RequestParam("id") Integer id, Model model) {
        Employee employeeById = employeeService.getEmployeeById(id);
        Collection<Departement> departments = departementService.getDepartments();
        model.addAttribute("departments", departments);
        model.addAttribute("emp", employeeById);
        System.out.println(employeeById.toString());
        System.out.println(departments.toString());
        return "/emp/update";
    }

    @PostMapping("/update")
    public String updateEmp(Employee employee) {
        employeeService.update(employee);
        System.out.println(employee.toString() + " ====   ======     ==");
        return "redirect:/emps";
    }

    @GetMapping("/delete")
    public String deleteEmp(@RequestParam("id") Integer id, Model model) {
        employeeService.delete(id);
        return "redirect:/emps";
    }

}
